home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Pure virtual destructors?
- Date: Thu, 18 Apr 1996 16:31:08 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4l5qmv$d0t@news.halcyon.com>
- References: <4kuq0i$p6t@ftp.ee.vill.edu> <3175BDF5.5ADC81B5@eiffel.com>
- NNTP-Posting-Host: blv-pm3-ip12.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- "Guus Leeuw jr." <guusl@eiffel.com> wrote:
-
- >sheridan@monet.vill.edu wrote:
- >>
- >> The rule with destructors is that they are always overridden, right?
- >> That's a quote from VC4 docs, BTW. When I do this:
- >>
- >> class CBase {
- >> public:
- >> virtual ~CBase() = 0;
- >> ...};
- >>
- >> class CChild : public CBase {
- >> public:
- >> ~CChild();
- >> ...};
- >>
- >> and declare a body for ~CChild, I always get unresolved external on
- >> CBase::~CBase. I have seen this in several cases. My guess is it's not
- >> a compiler bug, so what am I missing?
-
- >Here is what the working paper of the ANSI comittee on destructors says (sec.
- >12.4.6):
- >"Destructors are not inherited. A destructor can be declared virtual or pure
- >virtual; if any objects of that class or any derived class are created in the
- >program, the destructor shall be defined. If a class has a base class with a
- >virtual destructor, its destructor (whether user- or implicitly-declared) is
- >virtual."
-
- >Which means: You cannot have a pure virual destructor in CBase. It can be
- >virtual, though, for a virtual destructor still comes with a definition.
-
- >--
- >'l8r,
- > Guus
-
- You *can* have a pure virtual destructor in CBase. How did you
- possibly read that you couldn't from the text you quoted? You just
- have to give it a body.
-
- Giving a body to a pure-virtual method is *very* common and useful.
- Making the destructor pure-virtual is a convention many C++
- programmers use when they want the base class abstract, but every
- base-class method has a meaningful implementation. In other words,
- you don't want every derived class to have to type the same code for
- FuncX() so you don't dare make it pure-virtual, well make the dtor
- pure virtual.
-
- The following code compiles for me. If it doesn't for you, I'd
- upgrade your compiler.
-
- class A
- {
- public:
- A();
- virtual ~A() = 0;
-
- virtual void print()
- { cout << "A::print()\n"; }
- };
-
- class B : public A
- {
- public:
- B();
- virtual ~B();
-
- virtual void print()
- { cout << "B::print()\n"; }
- };
-
- A::A()
- { NULL; }
-
- A::~A()
- { cout << "~A\n"; }
-
- B::B() : A()
- { NULL; }
-
- B::~B()
- { cout << "~B\n"; }
-
- --Norm
-
-